﻿using Dapper;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Micro.Extensions.Data
{
    public static class DataExtensions
    {
        public static IServiceCollection AddMySql(this IServiceCollection services, IConfigurationSection section)
        {
            DefaultTypeMap.MatchNamesWithUnderscores = true;
            services.Configure<DataConfig>(section.GetSection("MySql"));
            services.TryAddSingleton<IDbConnectionFactory, DbConnectionFactory>();
            services.TryAddScoped<IDbContext, DbContext>();
            //简单仓储
            services.TryAddScoped(typeof(ICrudRepository<>), typeof(CrudRepository<>));
            return services;
        }
    }
}
